import React from 'react';
import { Table } from 'antd';
import Actions from 'src/components/Actions';
import { findVariableName, getVariableName, getVariableValue } from 'src/utils/variable';
import ResultStyle from './resultStyle';
import './index.less';

const RulesScore = (props) => {
  const { logs = {}, value, actions, variable, using_variables } = props;
  const { rules, variables } = logs;
  const isHasLog = Object.keys(logs).length > 0;
  const isHit = (id) => (rules.find((item) => item.id === id) || {}).hit;

  const rulesColumns = [
    {
      title: '序号',
      dataIndex: 'index',
      render: (_, __, index) => index + 1,
    },
    {
      title: '规则ID',
      dataIndex: 'rule_id',
    },
    {
      title: '规则名称',
      dataIndex: 'name',
    },
    {
      title: '左变量',
      dataIndex: 'condition.left_var_name',
      render: (_, { condition: { left_var_code, left_var_type, left_var_name } }) => {
        const tempVarName =
          using_variables && using_variables.length && using_variables.find((item) => item.code === left_var_code)
            ? getVariableName(left_var_code, using_variables)
            : findVariableName(left_var_type, left_var_code, variable);
        return isHasLog ? (
          <ResultStyle
            top={tempVarName}
            bottom={getVariableValue(
              { var_type: left_var_type, var_name: left_var_name, var_code: left_var_code },
              variables
            )}
          />
        ) : (
          tempVarName
        );
      },
    },
    {
      title: '操作符',
      dataIndex: 'condition.operator.symbol',
    },
    {
      title: '右变量',
      dataIndex: 'condition.right_var_name',
      render: (_, { condition: { right_var_code, right_var_type, right_var_name } }) => {
        const tempVarName =
          using_variables && using_variables.length && using_variables.find((item) => item.code === right_var_code)
            ? getVariableName(right_var_code, using_variables)
            : findVariableName(right_var_type, right_var_code, variable);
        return right_var_code ? (
          isHasLog ? (
            <ResultStyle
              top={tempVarName}
              bottom={getVariableValue(
                { var_type: right_var_type, var_name: right_var_name, var_code: right_var_code },
                variables
              )}
            />
          ) : (
            tempVarName
          )
        ) : (
          '-'
        );
      },
    },
    {
      title: '规则结果',
      dataIndex: 'result',
      render: (_, { id }) => {
        if (isHasLog) {
          const { hit } = rules.find((item) => item.id === id) || {};
          return <span className={hit ? 'hit' : 'not_hit'}>{JSON.stringify(hit)}</span>;
        }
        return;
      },
    },
    {
      title: '权重计算',
      dataIndex: 'hit',
      render: (_, { id }) => {
        if (isHasLog) {
          const { hit, actions } = rules.find((item) => item.id === id) || {};
          return hit && <span className='hit'>{actions[0].result}</span>;
        }
        return;
      },
    },
  ];

  const actionColums = [
    {
      title: '权重区间',
      dataIndex: 'min',
      width: '300px',
      render: (_, { min, max, id }) => {
        const range = `[${min}, ${max})`;
        return isHasLog ? (
          <ResultStyle
            tag
            hightLight
            isHit={isHit(id)}
            top={range}
            bottom={<span>总权重值：{getTotalWeight()}</span>}
          />
        ) : (
          range
        );
      },
    },
    {
      title: '动作',
      dataIndex: 'actions',
      render: (text, { id }) => (
        <Actions {...props} isHit={isHasLog && isHit(id)} value={text} disabled={true} actions={actions} />
      ),
    },
  ];

  // 获取总权重
  const getTotalWeight = () => {
    const var_code = `${logs.strategy_code},weight`;
    const totalWeight = getVariableValue({ var_code }, variables);
    return totalWeight === 'null' ? '' : totalWeight;
  };

  return (
    <>
      {isHasLog && (
        <div
          style={{
            marginBottom: '20px',
          }}
        >
          <span>
            总权重值:
            {<span>{getTotalWeight()}</span>}
          </span>
        </div>
      )}
      <Table
        columns={rulesColumns}
        dataSource={value.weight_calc_rules}
        rowKey='id'
        pagination={false}
        style={{ marginBottom: '20px' }}
      />
      <Table columns={actionColums} dataSource={value.weight_range_rules} rowKey='id' pagination={false} />
    </>
  );
};
export default RulesScore;
